package com.zbs.framework.common.utils.mp;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.function.Consumer;

/**
 * @author: Zbs
 * @createTime: 2020/12/18 14:54
 * 做一些非空判断，使业务代码块更加简洁
 */

@Getter
@Setter
public class MpUpdateWrapper<T> extends UpdateWrapper<T> {
    private UpdateWrapper<T> updateWrapper;


    public MpUpdateWrapper() {
        this.updateWrapper = new UpdateWrapper<>();
    }

    /**
     * 等于 =
     *
     * @param column 字段
     * @param val    值
     * @return MpQueryWrapper
     */
    public MpUpdateWrapper<T> setColumn(SFunction<T, ?> column, Object val) {
        super.set(ColumnMpUtil.getName(column), val);
        return this;
    }


    /**
     * 等于 =
     *
     * @param column 字段
     * @param val    值
     * @return MpQueryWrapper
     */
    public MpUpdateWrapper<T> eq(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.eq(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> eq(boolean condition, SFunction<T, ?> column, Object val) {
        super.eq(condition,ColumnMpUtil.getName(column), val);
        return this;
    }

    /**
     * 不等于 <>
     *
     * @param column 字段
     * @param val    值
     * @return MpQueryWrapper
     */
    public MpUpdateWrapper<T> ne(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.ne(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> ne(boolean condition, SFunction<T, ?> column, Object val) {
        super.ne(condition,ColumnMpUtil.getName(column), val);
        return this;
    }

    /**
     * 大于 >
     *
     * @param column 字段
     * @param val    值
     * @return MpQueryWrapper
     */
    public MpUpdateWrapper<T> gt(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.gt(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> gt(boolean condition, SFunction<T, ?> column, Object val) {
        super.gt(condition,ColumnMpUtil.getName(column), val);
        return this;
    }


    /**
     * 大于等于 >=
     *
     * @param column 字段
     * @param val    值
     * @return MpQueryWrapper
     */
    public MpUpdateWrapper<T> ge(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.ge(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> ge(boolean condition, SFunction<T, ?> column, Object val) {
        super.ge(condition,ColumnMpUtil.getName(column), val);
        return this;
    }

    /**
     * 小于 <
     *
     * @param column 字段
     * @param val    值
     * @return MpQueryWrapper
     */
    public MpUpdateWrapper<T> lt(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.lt(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> lt(boolean condition, SFunction<T, ?> column, Object val) {
        super.lt(condition,ColumnMpUtil.getName(column), val);
        return this;
    }

    /**
     * 小于等于 <=
     *
     * @param column 字段
     * @param val    值
     * @return MpQueryWrapper
     */
    public MpUpdateWrapper<T> le(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.le(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> le(boolean condition, SFunction<T, ?> column, Object val) {
        super.le(condition,ColumnMpUtil.getName(column), val);
        return this;
    }

    public MpUpdateWrapper<T> like(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.like(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> like(boolean condition, SFunction<T, ?> column, Object val) {
        super.like(condition,ColumnMpUtil.getName(column), val);
        return this;
    }

    public MpUpdateWrapper<T> notLike(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.notLike(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> notLike(boolean condition, SFunction<T, ?> column, Object val) {
        super.notLike(condition,ColumnMpUtil.getName(column), val);
        return this;
    }

    /**
     * LIKE '%值'
     *
     * @param column 字段
     * @param val    值
     * @return MpQueryWrapper
     */
    public MpUpdateWrapper<T> likeLeft(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.likeLeft(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> likeLeft(boolean condition, SFunction<T, ?> column, Object val) {
        super.likeLeft(condition,ColumnMpUtil.getName(column), val);
        return this;
    }

    /**
     * LIKE '值%'
     *
     * @param column 字段
     * @param val    值
     * @return MpQueryWrapper
     */
    public MpUpdateWrapper<T> likeRight(SFunction<T, ?> column, Object val) {
        if (!ObjectUtils.isEmpty(val)) {
            super.likeRight(ColumnMpUtil.getName(column), val);
        }
        return this;
    }

    public MpUpdateWrapper<T> likeRight(boolean condition, SFunction<T, ?> column, Object val) {
        super.likeRight(condition,ColumnMpUtil.getName(column), val);
        return this;
    }

    public MpUpdateWrapper<T> between(SFunction<T, ?> column, Object val1, Object val2) {
        super.between(ColumnMpUtil.getName(column), val1, val2);
        return this;
    }

    public MpUpdateWrapper<T> between(boolean condition, SFunction<T, ?> column, Object val1, Object val2) {
        super.between(condition,ColumnMpUtil.getName(column), val1, val2);
        return this;
    }

    public MpUpdateWrapper<T> or(Consumer<UpdateWrapper<T>> consumer) {
        super.or(consumer);
        return this;
    }

    public MpUpdateWrapper<T> and(Consumer<UpdateWrapper<T>> consumer) {
        super.and(consumer);
        return this;
    }

    public MpUpdateWrapper<T> notBetween(SFunction<T, ?> column, Object val1, Object val2) {
        super.notBetween(ColumnMpUtil.getName(column), val1, val2);
        return this;
    }

    public MpUpdateWrapper<T> notBetween(boolean condition, SFunction<T, ?> column, Object val1, Object val2) {
        super.notBetween(condition,ColumnMpUtil.getName(column), val1, val2);
        return this;
    }


    public MpUpdateWrapper<T> isNull(SFunction<T, ?> column) {
        super.isNull(ColumnMpUtil.getName(column));
        return this;
    }

    public MpUpdateWrapper<T> isNotNull(SFunction<T, ?> column) {
        super.isNotNull(ColumnMpUtil.getName(column));
        return this;
    }

    public MpUpdateWrapper<T> in(SFunction<T, ?> column, Object... values) {
        if (values != null && values.length > 0) {
            super.in(ColumnMpUtil.getName(column), values);
        }
        return this;
    }

    public MpUpdateWrapper<T> in(boolean condition,SFunction<T, ?> column, Object... values) {
        super.in(condition,ColumnMpUtil.getName(column), values);
        return this;
    }

    public MpUpdateWrapper<T> in(SFunction<T, ?> column, Collection<?> coll) {
        if (!CollectionUtils.isEmpty(coll)) {
            super.in(ColumnMpUtil.getName(column), coll);
        }
        return this;
    }

    public MpUpdateWrapper<T> in(boolean condition,SFunction<T, ?> column,Collection<?> coll) {
        super.in(condition,ColumnMpUtil.getName(column), coll);
        return this;
    }

    public MpUpdateWrapper<T> notIn(SFunction<T, ?> column, Object... values) {
        if (values != null && values.length > 0) {
            super.notIn(ColumnMpUtil.getName(column), values);
        }
        return this;
    }

    public MpUpdateWrapper<T> notIn(boolean condition,SFunction<T, ?> column, Object... values) {
        super.notIn(condition,ColumnMpUtil.getName(column), values);
        return this;
    }

    public MpUpdateWrapper<T> notIn(boolean condition,SFunction<T, ?> column, Collection<?> coll) {
        super.notIn(condition,ColumnMpUtil.getName(column), coll);
        return this;
    }

    public MpUpdateWrapper<T> notIn(SFunction<T, ?> column, Collection<?> coll) {
        if (!CollectionUtils.isEmpty(coll)) {
            super.notIn(ColumnMpUtil.getName(column), coll);
        }
        return this;
    }



    public MpUpdateWrapper<T> having(String sqlHaving, Object... params) {
        super.having(sqlHaving, params);
        return this;
    }

    public MpUpdateWrapper<T> func(Consumer<UpdateWrapper<T>> consumer) {
        super.func(consumer);
        return this;
    }
}
